<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    public $pkString = "";
    public $modelName = "";
    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    
    public $headTitle ;
    
    /**
     *
     * @var array the navigation common to almost all controller
     */
    public $navigation = array(
        'ADD' => 'create',
        'LIST' => 'admin',
//        'TRASH' => 'deletedlist',
    );

    protected function beforeAction($action) {
        return parent::beforeAction($action);
    }
    
    
    
    public function actionAdmin($isTrashed = 0) {
        $model = new $this->modelName('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET[$this->modelName]))
            $model->attributes = $_GET[$this->modelName];

        $template = Constant::GRIDVIEW_CBUTTONCOLUMN_ADMIN_TEMPLATE;
        $this->render('admin', array(
            'model' => $model,
            'isTrashed' => $isTrashed,
            'template' => $template,
        ));
    }
    
    public function actionDeletedList($isTrashed = 1) {
        $model = new $this->modelName('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET[$this->modelName]))
            $model->attributes = $_GET[$this->modelName];

        $template = Constant::GRIDVIEW_CBUTTONCOLUMN_DELETED_LIST_TEMPLATE;
        $this->render('admin', array(
            'model' => $model,
            'isTrashed' => $isTrashed,
            'template' => $template,
        ));
    }
    
    public function actionTrash($id) {
        if (Yii::app()->request->isPostRequest) {
            $model = $this->loadModel($id);
            $model->isDeleted = 1;

            $model->save(false);

            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        elseif (isset($_GET['id'])) {
            $model = $this->loadModel($_GET['id']);
            $model->isDeleted = 1;

            $model->save(false);

            $this->redirect(array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }
    
    public function actionUntrash($id) {
        if (Yii::app()->request->isPostRequest) {
            $model = $this->loadModel($id);
            $model->isDeleted = 0;

            $model->save(false);

            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        elseif (isset($_GET['id'])) {
            $model = $this->loadModel($_GET['id']);
            $model->isDeleted = 0;

            $model->save(false);

            $this->redirect(array('deletedlist'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }
    
//    public function actionUntrash($id) {
//        var_dump($_GET);exit();
//        if (Yii::app()->request->isPostRequest) {
//            $model = $this->loadModel($id);
//            $model->isDeleted = 0;
//
//            $model->save(false);
//
//            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
//            if (!isset($_GET['ajax']))
//                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
//        }
//        elseif (isset($_GET[$this->pkString])) { 
//            $id = $_GET[$this->pkString];
//            $model = $this->loadModel($id); 
//            $model->isDeleted = 0;
//            
//            $model->save(false);
//
//            $this->redirect(array('admin'));
//        }
//        else
//            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
//    }

}